package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.StageBilling;
import com.woniu.mapper.StageBillingMapper;
import com.woniu.service.StageBillingService;
import com.woniu.vo.StageBillingVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Time;
import java.util.List;

/**
* @author Apricity
* @description 针对表【stage_billing(阶段计费表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:59
*/
@Service
public class StageBillingServiceImpl extends ServiceImpl<StageBillingMapper, StageBilling>
    implements StageBillingService {

    @Resource
    private StageBillingMapper stageBillingMapper;

    /**
     * 获取分页的舞台计费列表
     *
     * @param keyword  搜索关键字
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 舞台计费列表
     */
    @Override
    public List<StageBillingVO> getStageBillingList(String keyword, Integer pageNum, Integer pageSize) {
        // 初始化分页对象
        Page<StageBillingVO> page = new Page<>(pageNum, pageSize);
        List<StageBillingVO> stages;

        // 根据关键字查询舞台计费列表
        if (keyword != null && !keyword.trim().isEmpty()) {
            stages = stageBillingMapper.queryStageBillingByKeyword(keyword, page);
        } else {
            // 查询所有舞台计费列表
            stages = stageBillingMapper.queryStageBillingList(page);
        }

        return stages;
    }

    /**
     * 添加舞台计费信息
     *
     * @param stageBilling 舞台计费对象
     * @return 添加成功返回true，否则返回false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addStageBilling(StageBilling stageBilling) {
        // 如果是全时段，设置时间为全天
        if (stageBilling.getIsAllDay() != null && stageBilling.getIsAllDay() == 1) {
            stageBilling.setStartTime(Time.valueOf("00:00:00"));
            stageBilling.setEndTime(Time.valueOf("23:59:59"));
        }

        // 设置默认值
        if (stageBilling.getElectricityFee() == null) {
            stageBilling.setElectricityFee(BigDecimal.ZERO);
        }
        if (stageBilling.getChargingServiceFee() == null) {
            stageBilling.setChargingServiceFee(BigDecimal.ZERO);
        }

        // 插入舞台计费信息
        return stageBillingMapper.insertStageBilling(stageBilling) > 0;
    }

    /**
     * 删除舞台计费信息
     *
     * @param id 舞台计费信息的ID
     * @return 删除成功返回true，否则返回false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStageBilling(Long id) {
        // 根据ID删除舞台计费信息
        return stageBillingMapper.deleteById(id) > 0;
    }

    /**
     * 更新舞台计费信息
     *
     * @param stageBilling 舞台计费对象
     * @return 更新成功返回true，否则返回false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStageBilling(StageBilling stageBilling) {
        // 检查ID是否为空
        if (stageBilling.getId() == null) {
            return false;
        }
        // 更新舞台计费信息
        return stageBillingMapper.updateStageBilling(stageBilling) > 0;
    }
}





